<?php

if ( $_GET['graph'] == 'mapcompletion' )
{
	// TODO: Security flaw!
	
	include('scripts/'.$_GET['game'].'.php');
	
	$game = $_GET['game'];
	
	$register_slot = sql("SELECT `progress`, COUNT(`progress`) AS howmany FROM `distribution_register` WHERE `product` = '".$game."' GROUP BY `progress`");
		
	// Put together the numbers!
	
	$counter=0;
	$total=0;
	do
	{
		$array[$sql_row[$register_slot]['progress']] = $sql_row[$register_slot]['howmany'];
		$array_index[$counter] = $sql_row[$register_slot]['progress'];
		$total += $sql_row[$register_slot]['howmany'];
		$counter+=1;
		sql_next($register_slot);
	}
	while ($counter<$sql_total[$register_slot]);
	
	$w = 768;
	$h = 256;
	$fw = $w-64-64;
	$fh = $h-16-16;
	$ft = 16;
	$fl = 64;
	
	/* Create a black image */
	$img			=	imagecreatetruecolor($w, $h);
	$cbackground	=	imagecolorallocate($img, 255, 255, 255);
	$ctext			=	imagecolorallocate($img, 0, 0, 0);
	$cborder		=	imagecolorallocate($img, 32, 32, 32);
	$font			=	'private/verdana0.ttf';
	
	
	
	imagefilledrectangle($img, 0, 0, $w, $h,$cbackground);
	
	imagefilledrectangle($img, $fl, $ft, $fl, $ft+$fh,$cborder);
	imagefilledrectangle($img, $fl-16, $ft, $fl, $ft,$cborder);
	imagefilledrectangle($img, $fl-16, $ft+$fh/2, $fl, $ft+$fh/2,$cborder);
	imagefilledrectangle($img, $fl-16, $ft+$fh, $fl+$fw, $ft+$fh,$cborder);
	
	
	imagettftext($img, 10, 0, 4, $ft, $ctext, $font, '100%');
	imagettftext($img, 10, 0, 16, $ft+$fh/2, $ctext, $font, '50%');
	imagettftext($img, 10, 0, 16, $ft+$fh, $ctext, $font, '0%');
	
	$counter=0;
	$left = $total;
	do
	{
		if ( !isset($array[$counter]))
		{
			$array[$counter] = 0;
		}
		
		$percent = $left/$total;
		
		
		$cmap			=	imagecolorallocate($img, $percent*128+(1-$percent)*255, $percent*255+(1-$percent)*128, $percent*128+(1-$percent)*128);
		
		$percent*=100;
		
		if ( $left == $total )
		{
			$offset = -8;
		}
		else
		{
			$offset = -3;
		}
			
		imagefilledrectangle($img , $fl+(($counter+1)/count($progress_id_to_value[$game])*$fw)-20, $ft+(1-$left/$total)*$fh,$fl+(($counter+1)/count($progress_id_to_value[$game])*$fw)-4,$ft+$fh-1,$cmap);
		imagettftext($img, 10, -90, $fl+(($counter+1)/count($progress_id_to_value[$game])*$fw)-16, $ft, $ctext, $font, $progress_id_to_value[$game][$counter] . ' (' . $left . ')' );
		imagettftext($img, 10, 0, $offset+$fl+(($counter+1)/count($progress_id_to_value[$game])*$fw)-20, $ft+$fh+16, $ctext, $font, (int)$percent . '%');
		
		
		$left-=$array[$counter];
		$counter+=1;
	}
	while ($counter<count($progress_id_to_value[$game]));
	
	clearslot($register_slot);
}
elseif ($_GET['graph']=='showdownloads')
{
	if (isset($_GET['game']))
	{
		$gamestr	=	'WHERE product = "'.$_GET['game'].'"';
	}
	$request		=	'SELECT count(*) as count, YEAR(DATE(d.installed)) as year, WEEK((DATE(d.installed))) as week FROM `distribution_register` d '.$gamestr.' GROUP BY YEARWEEK(DATE(d.installed)) ORDER BY year DESC, week DESC LIMIT 0, 16';
	$register_slot	=	sql($request);
	
	$counter=0;
	$total=0;
	do
	{
		$array[$counter]['week']	=	$sql_row[$register_slot]['week'];
		$array[$counter]['year']	=	$sql_row[$register_slot]['year'];
		$array[$counter]['count']	=	$sql_row[$register_slot]['count'];
		if ( $total < $sql_row[$register_slot]['count'] )
		{
			$total	=	$sql_row[$register_slot]['count'];			
		}
		$counter+=1;
		sql_next($register_slot);
	}
	while ($counter<$sql_total[$register_slot]);
	
	$w = 768;
	$h = 256;
	$fw = $w-64-64;
	$fh = $h-16-16;
	$ft = 16;
	$fl = 64;
	
	/* Create a black image */
	$img			=	imagecreatetruecolor($w, $h);
	$cbackground	=	imagecolorallocate($img, 255, 255, 255);
	$ctext			=	imagecolorallocate($img, 0, 0, 0);
	$cborder		=	imagecolorallocate($img, 32, 32, 32);
	$font			=	'private/verdana0.ttf';
	
	
	
	imagefilledrectangle($img, 0, 0, $w, $h,$cbackground);
	
	imagefilledrectangle($img, $fl, $ft, $fl, $ft+$fh,$cborder);
	imagefilledrectangle($img, $fl-16, $ft, $fl, $ft,$cborder);
	imagefilledrectangle($img, $fl-16, $ft+$fh/2, $fl, $ft+$fh/2,$cborder);
	imagefilledrectangle($img, $fl-16, $ft+$fh, $fl+$fw, $ft+$fh,$cborder);
	
	
	imagettftext($img, 10, 0, 16, $ft, $ctext, $font, (int)($total));
	imagettftext($img, 10, 0, 16, $ft+$fh/2, $ctext, $font, (int)($total/2));
	imagettftext($img, 10, 0, 16, $ft+$fh, $ctext, $font, '0');
	
	$counter=0;
	$left = $total;
	do
	{		
		$lookup			=	count($array)-1-$counter;	
		$percent		=	$array[$lookup]['count']/$total;		
		$cmap			=	imagecolorallocate($img, $percent*128+(1-$percent)*255, $percent*255+(1-$percent)*128, $percent*128+(1-$percent)*128);
					
		imagefilledrectangle($img,$fl+(($counter+1)/count($array)*$fw)-20, $ft+(1-$percent)*$fh,$fl+(($counter+1)/count($array)*$fw)-4,$ft+$fh-1,$cmap);
		
		$percent*=100;
		
		imagettftext($img, 10, -90, $fl+(($counter+1)/count($array)*$fw)-16, $ft, $ctext, $font, 'Week '.$array[$lookup]['week'].' '.$array[$lookup]['year'] );
		imagettftext($img, 10, 0, $fl+(($counter+1)/count($array)*$fw)-20, $ft+$fh+16, $ctext, $font, $array[$lookup]['count']);
		
		$counter+=1;
	}
	while ($counter < count($array));

	
	clearslot($register_slot);
}



// Add some shadow to the text
//imagettftext($img, 10, -90, 11, 21, $ctext, $font, 'OHLOLOLOLOLHAX');

header('Content-Type: image/png');

imagepng($img);
imagedestroy($img);
?> 
